package com.shunan.miaosha.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


/**
 * 需要可以将任意的类型转化为string类型，并且可以根据字符串还原出对象
 * 
 * get,set方法 调用时，key都是 加上前缀后的key，并且可以设置过期时间。
 */
@Service
public class RedisService {
	
	@Autowired
	private JedisPool jedisPool;
	
	
	public <T> T get(KeyPrefix prefix,String key,Class<T> clazz){
		Jedis j=null;
		try{
			j=jedisPool.getResource();				
			
			return Key2String.string2Key(j.get(prefix.getPrefix()+key) , clazz);
		}finally{
			j.close();
		}
	}
	
	public <T> void set(KeyPrefix prefix,String key,T value){
		Jedis j=null;
		try{
			j=jedisPool.getResource();				
			String v=Key2String.key2String(value);
			j.set(prefix.getPrefix()+key, v);
		}finally{
			j.close();
		}
	}
	
/*	public Object getResource(String key){
		Jedis j=null;
		try{
			j=jedisPool.getResource();				
			
			return j.get(key);
		}finally{
			j.close();
		}
	}
	
	public void setResource(String key,String value){
		Jedis j=null;
		try{
			j=jedisPool.getResource();			
			j.set(key, value);
		}finally{
			j.close();
		}
	}*/
	
}
